using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class RecruitManagement_JobCategory : System.Web.UI.Page
{
    string connectionString =
                WebConfigurationManager.ConnectionStrings[
                "RCVDB2000"].ConnectionString;
    SqlConnection sqlConnection ;
    SqlCommand sqlCommand;
    protected void Page_Load(object sender, EventArgs e)
    {           
        if(!IsPostBack)
        {
            string itemId = Request.QueryString.Get("ItemId");
            if(itemId!=null)
            { 
                EditData(itemId);
            }
            else
                CreateNewID();
           
        }
    }
    
    #region Custom methods
    void AddParameters(SqlCommand sqlCommand, 
    string[] parameterCollection, 
    string[] valueCollection)
    {
        for (int i = 0; i < parameterCollection.Length; i++)
        {
            sqlCommand.Parameters.AddWithValue(
            parameterCollection[i], valueCollection[i]);
        }
    }
    void EditData(string itemId)
    {
        btnSave.Text = "Update";
        literalHeader.Text = "Update Job Category";
        if(itemId != null) 
        {
            Common cls = new Common();
            itemId= cls.RCVQueryStringDecrypting(itemId);
            sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
            connectionString;
            try
            {
                sqlConnection.Open();
                sqlCommand = new SqlCommand();
                sqlCommand.CommandText =
                "select * from tblJobCategories"
                +" where JobCategoryID='" + itemId  + "'";
                sqlCommand.Connection = sqlConnection;
                sqlCommand.CommandType = CommandType.Text;
                SqlDataReader sqlDataReader 
                = sqlCommand.ExecuteReader();
                 
                if(sqlDataReader.Read())
                {
                    txtID.Text = itemId;
                    txtName.Text =
                    Convert.ToString(sqlDataReader[1]);
                }
                sqlCommand.Dispose();
                sqlConnection.Close();
            }
            catch (Exception ex)
            {
                literalError.Text += "Failed:" + ex.Message;
            }
            sqlConnection.Dispose(); 
        }
    }
    void CreateNewID()
    {
        btnSave.Text="Save";
        literalHeader.Text = "Add New Job Category";
        txtID.Text = "";
        txtName.Text = "";
        sqlConnection = new SqlConnection();
        sqlConnection.ConnectionString =
        connectionString;
        try
        {
            sqlConnection.Open();
            sqlCommand = new SqlCommand();
            sqlCommand.CommandText = 
            "select Max(JobCategoryID) + 1" +
            " from tblJobCategories";
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandType = CommandType.Text;             
            txtID.Text = 
                sqlCommand.ExecuteScalar().ToString();           
            sqlCommand.Dispose();
            sqlConnection.Close();
        }
        catch (Exception ex)
        {
            literalError.Text += "Failed:" + ex.Message;
        }
        sqlConnection.Dispose(); 
    }
    #endregion
    
    protected void btnSave_Click(object sender, EventArgs e)
    {
        sqlConnection = new SqlConnection();
        sqlConnection.ConnectionString =
        connectionString;
        try
        {
            sqlConnection.Open();
            sqlCommand = new SqlCommand();
            if(btnSave.Text=="Save")  
            {           
                sqlCommand.CommandText =
                "Insert into tblJobCategories"+
                " values (@ID,@Name)";   
            } 
            else
            {
                sqlCommand.CommandText =
                "Update tblJobCategories" +
                " set JobCategoryName=@Name"+
                " where JobCategoryID=@ID";          
            }
            AddParameters(sqlCommand,
            new string[2]{"@ID","@Name"},
            new string[2]{txtID.Text,txtName.Text});
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandType = CommandType.Text;
            if(sqlCommand.ExecuteNonQuery()>0)
            {
                if(btnSave.Text=="Save")  
                {
                    literalError.Text = 
                    "New record has been inserted.";
                    CreateNewID();
                }
                else
                {
                    literalError.Text =
                    "Record has been updated.";
                    Response.Redirect("JobCategories.aspx");
                }
            }
            sqlCommand.Dispose();
            sqlConnection.Close();
        }
        catch (Exception ex)
        {            
            literalError.Text = 
            "<br>Failed:" + ex.Message;
        }
        sqlConnection.Dispose();
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        CreateNewID();
    }
}
